//
//  CXShopFoodViewController.m
//  Meituan_OC
//
//  Created by Tony on 16/9/27.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CXShopFoodViewController.h"
#import "CXShopFoodCategoryModel.h"
#import "CXShopFoodCell.h"
#import "CXShopFoodHeader_section.h"
#import "CXShopFoodListCell.h"

static NSString * cellId = @"cellId";
static NSString * vHeaderId = @"vHeaderId";

@interface CXShopFoodViewController () <UITableViewDataSource, UITableViewDelegate>

/// 菜品分类数据
@property (nonatomic, strong) NSArray<CXShopFoodCategoryModel *> * foodCategoryModelList;

@property (nonatomic, strong) UITableView * tvCategory;

@property (nonatomic, strong) UITableView * tvFoodList;

@end

@implementation CXShopFoodViewController {
    
    /// 菜品分类数据
//    NSArray<CXShopFoodCategoryModel *> * _foodCategoryModelList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setupUI];
    
    // 分类标签tv默认选中第0行
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_tvCategory selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - tableView dataSource and delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _tvCategory) {
        return 1;
    }else {
        return self.foodCategoryModelList.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _tvCategory) {
        return self.foodCategoryModelList.count;
    } else {
        return self.foodCategoryModelList[section].spus.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    if (tableView == _tvCategory) {
        cell.textLabel.text = self.foodCategoryModelList[indexPath.row].name;
        
    } else {
//        cell.textLabel.text = self.foodCategoryModelList[indexPath.section].spus[indexPath.row].name;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tvCategory) {
        // 详情列表tableView滚动
        NSIndexPath * path = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        [self.tvFoodList scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
    } else {
        
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == _tvCategory) {
        return nil;
    }
    
    // 1. 取到视图
    CXShopFoodHeader_section * vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:vHeaderId];
    
    // 2. 设置属性
    vHeader.titleLabel.text = self.foodCategoryModelList[section].name;
    
    // 3. 返回header视图
    return vHeader;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tvCategory) {
        return;
    }
    
    NSIndexPath * iPath = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
    [_tvCategory selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionTop];
}

#pragma mark - setupUI
-(void) setupUI {
    // 创建两个tableView
    _tvCategory = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tvCategory.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tvCategory];
    
    _tvFoodList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tvFoodList.backgroundColor = [UIColor greenColor];
    _tvFoodList.rowHeight = 120;
    
    // 给组头设置高度，否则 viewForHeaderInSection 代理方法不起作用
    _tvFoodList.sectionHeaderHeight = 23;
    [self.view addSubview:_tvFoodList];
    
    // 自动布局
    [_tvCategory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.view);
        make.width.mas_equalTo(86);
    }];
    
    [_tvFoodList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.bottom.equalTo(self.view);
        make.leading.mas_equalTo(_tvCategory.mas_trailing);//.mas_offset(9);
    }];
    
    // 设置代理和数据源
    self.tvCategory.dataSource = self;  
    self.tvFoodList.dataSource = self;
    
    self.tvCategory.delegate = self;
    self.tvFoodList.delegate = self;
    
    // 注册cell
    [_tvCategory registerClass:[CXShopFoodCell class] forCellReuseIdentifier:cellId];
    [_tvFoodList registerClass:[CXShopFoodListCell class] forCellReuseIdentifier:cellId];
    // 注册组头
    [_tvFoodList registerClass:[CXShopFoodHeader_section class] forHeaderFooterViewReuseIdentifier:vHeaderId];
    
}


#pragma mark - lazy

-(NSArray<CXShopFoodCategoryModel *> *)foodCategoryModelList {
    if (!_foodCategoryModelList) {
        
        NSDictionary * resultDict = [self dictWithJOSN];
        
//        [resultDict writeToFile:@"/Users/Tony/Desktop/food.plist" atomically:YES];
        
        NSArray * food_spu_tags = resultDict[@"data"][@"food_spu_tags"];
        
        _foodCategoryModelList = [CXShopFoodCategoryModel dataArrayWithArr:food_spu_tags];
    }
    return _foodCategoryModelList;
}

- (NSDictionary *)dictWithJOSN {
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"food.json" withExtension:nil];
    NSData * data = [NSData dataWithContentsOfURL:url];
    return [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
}

@end
